// TODO 刷新后初始化window数据
export const initWindwoData = (data) => {
    Object.keys(data).map(key => window[key] = data[key])
}


// TODO 解url参数
export const parseURLParams = url => {
    const urlParams = new URLSearchParams(url)
    const params = {}
    for (const [key, value] of urlParams.entries()) { params[key] = value }
    return params
}

// TODO 广度遍历
export const breadthSearch = (tree, text, url) => {
    var stark = []
    stark = stark.concat(tree)
    while (stark.length) {
        var temp = stark.shift()
        if (temp?.children) stark = stark.concat(temp.children)
        if (temp[text] === url) return temp
    }
}

// TODO 获取字符串中的uuid
export const getStringUuid = (string) => {
    const uuidRegex = /[a-f0-9]{8}-[a-f0-9]{4}-[4][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}/ig
    return string.match(uuidRegex)
}

// TODO 树形数据获取value (自身数据)
export const findValue = (tree, value, key = 'uuid') => {
    for (let i = 0; i < tree.length; i++) {
        if (tree[i][key] === value) {
            return tree[i]
        } else if (tree[i].children && tree[i].children.length) {
            const result = findValue(tree[i].children, value, key)
            if (result) return result
        }
    }
    return null
}
// TODO 树形数据获取value (父级数据)
export const findParentNode = (treeData, value, key = 'uuid') => {
    for (let item of treeData) {
        if (item.children && item.children.length > 0) {
            if (item.children.some(obj => obj[key] === value)) {
                return item
            } else findParentNode(item.children, value, key)
        }
    }
    return null
}


// TODO 替换树形中的指定数据
export const replaceObjectByKey = (treeArray, value, newObject, key = 'uuid') => {
    return treeArray.map(node => {
        if (node[key] === value) {
            return { ...newObject }; // 使用扩展运算符创建一个新对象，替换原对象
        } else if (node.children && node.children.length > 0) {
            return { ...node, children: replaceObjectByKey(node.children, value, newObject, key) }; // 递归处理子节点
        } else {
            return node;
        }
    })
}

// TODO 删除树形中的指定对象
export const removeObjectByKey = (treeArray, value, key = 'uuid') => {
    return treeArray.filter(node => {
        if (node[key] === value) {
            return false;
        } else if (node.children && node.children.length > 0) {
            node.children = removeObjectByKey(node.children, value, key)
        }
        return true;
    });
}

// TODO 根据指定key值替换数组对象
export const replaceObjectByKeyTwo = (array, key, newObject) => {
    return array.map((obj) => obj[key] === newObject[key] ? newObject : obj);
};

// TODO 时间转换 dayjs -> YYYY-MM-DD
export const dayToString = (date, item) => {
    const { picker, showTime } = item
    const dateString = 'YYYY-MM-DD HH:mm:ss'
    const timeList = { year: 'YYYY', month: 'YYYY-MM', date: 'YYYY-MM-DD', time: 'HH:mm:ss' }
    if (picker === 'date' && showTime) return date.format(dateString)
    else return date.format(timeList[picker])
}


// TODO 数字转金额
export const numberToMoney = (number) => {
    return new Intl.NumberFormat('en-US', { style: 'decimal' }).format(number)
}

// TODO 金额转数字
export const moneyToNumber = (money) => {
    return Number(money.replace(/[^0-9.-]+/g, ""))
}
